#encoding:utf-8
'''
飞机大战
导入游戏开发所需要的包文件
包括：pygame，time，time
'''

import pygame
import time
import random
from pygame.locals import *
class _shell:
	'''定义炮弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x+53
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/pd.png")
	def display(self):
		'''绘制炮弹'''
		# 绘制炮弹
		self.screen.blit(self.image,(self.x,self.y))
	def move(self):
		'''玩家飞机-炮弹移动'''
		self.y -= 10
		if self.y <= -20:
			return True
	def emenmove(self):
		'''敌机炮弹移动'''
		self.y+=10
		if self.y >=570:
			return True;

class Hero_plane:
	'''定义玩家飞机类'''
	def __init__(self,screen_temp):
		self.x = 200
		self.y = 468
		self.screen = screen_temp
		self.image = pygame.image.load("./images/me.png")
		self._shell_list = [] #玩家的子弹列表
	def display(self):
		'''绘制子弹'''
		for b in self._shell_list:
			b.display()
			if b.move():
				self._shell_list.remove(b)
		'''绘制玩家飞机'''
		self.screen.blit(self.image,(self.x,self.y))
	def move_left(self):
		'''飞机左移动'''
		self.x -= 5
		if self.x <= 0:
			self.x = 0
	def move_right(self):
		'''飞机右移动'''
		self.x += 5
		if self.x >= 406:
			self.x = 406
	def fire(self):
		'''定义发射方法'''
		self._shell_list.append(_shell(self.screen,self.x,self.y))
		print(len(self._shell_list))

	def collision(self,temp_emen):
		'''碰撞检测'''
		if temp_emen.x > self.x+5 and temp_emen.x < self.x+93 and temp_emen.y > self.y+5 and temp_emen.y < self.y+80:
			return True;

class emey_plane:
	'''定义敌机类'''
	def __init__(self,screen_temp):
		self.x = random.choice(range(408))
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./images/e"+str(random.choice(range(3)))+".png")
		self._shell_list = []
	def display(self):
		'''绘制敌机'''
		for i in self._shell_list:
			i.display()
			if i.emenmove():
				self._shell_list.remove(i)
		self.screen.blit(self.image,(self.x,self.y))
	def move(self,hero):
		self.y += 4
		# 敌机出屏幕
		if self.y > 568:
			return True
		# 遍历所有子弹
		for p in hero._shell_list:
			if p.x > self.x+12 and p.x < self.x+92 and p.y >self.y+20 and p.y < self.y+60:
				hero._shell_list.remove(p)
				return True
	def emey_fire(self):
		'''定义敌机发射炮弹'''
		if random.choice(range(50)) == 10:
			self._shell_list.append(_shell(self.screen,self.x,self.y))

def key_control(hero):
	'''定义键盘控制函数'''
	# 1.执行退出操作
	for key in pygame.event.get():
		if key.type == QUIT:
			print("exit()...")
			exit()
	# 2.获取键盘信息
	pressed_list = pygame.key.get_pressed()
	# 3.做判断并执行对象的操作
	if pressed_list[K_LEFT] or pressed_list[K_a]:
		print("left...")
		hero.move_left()
	elif pressed_list[K_RIGHT] or pressed_list[K_d]:
		print("right...")
		hero.move_right()

	if pressed_list[K_SPACE]:
		print("space...")
		hero.fire()
	


def main():
	'''定义用来执行程序的主函数'''
	# 1.定义游戏主窗口
	screen = pygame.display.set_mode((512,568),0,0)
	# 2.定义游戏主界面
	background = pygame.image.load("./images/bg2.jpg")
	# 创建玩家飞机
	_hero = Hero_plane(screen)
	# 定义存放敌机列表
	_emey_list = []
	_game = True

	# 定义爆炸画面变量
	blast_1 = pygame.image.load("./images/b1.png")
	blast_2 = pygame.image.load("./images/b2.png")
	blast_3 = pygame.image.load("./images/b3.png")
	blast_4 = pygame.image.load("./images/b4.png")

	m = -968
	while True:
		# 3.绘制游戏主界面
		screen.blit(background,(0,m))
		m+=2
		if m >= -200:
			m = -968

		#绘制玩家飞机
		if _game:
			_hero.display()
		# 调用键盘控制函数
		key_control(_hero)
		# 随机绘制敌机
		if random.choice(range(50)) == 15:
			_emey_list.append(emey_plane(screen))
		# 遍历敌机并绘制移动
		for em in _emey_list:
			em.display()
			em.emey_fire()
			if em.move(_hero):
				_emey_list.remove(em)
				# 绘制爆炸场面
				screen.blit(blast_1,(em.x+50,em.y))
				screen.blit(blast_2,(em.x+50,em.y))
				screen.blit(blast_3,(em.x+50,em.y))
				screen.blit(blast_4,(em.x+50,em.y))
			if _hero.collision(em):
				'''敌机与玩家飞机相撞后爆炸场面'''
				# 绘制爆炸场面
				screen.blit(blast_1,(_hero.x+53,_hero.y+50))
				screen.blit(blast_2,(_hero.x+50,_hero.y+50))
				screen.blit(blast_3,(_hero.x+50,_hero.y+50))
				screen.blit(blast_4,(_hero.x+50,_hero.y+50))
				_emey_list.remove(em)
				# _hero.remove()
				_game = False
			for i in em._shell_list:
				'''敌机击打玩家飞机'''
				if _hero.collision(i):
					screen.blit(blast_1,(_hero.x+53,_hero.y+50))
					screen.blit(blast_2,(_hero.x+50,_hero.y+50))
					screen.blit(blast_3,(_hero.x+50,_hero.y+50))
					screen.blit(blast_4,(_hero.x+50,_hero.y+50))
					em._shell_list.remove(i)
					_game = False
		# 4.更新显示
		pygame.display.update()
		# 5.定时显示
		time.sleep(0.04)

# 判断当前程序为主运行程序是调用游戏主函数
if __name__ == '__main__':
	main()
